//
//  HQStyleSearchViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/12.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQStyleSearchViewController.h"
#import "HQStyleSearchCell.h"
#import "HQAddStyleModulView.h"
#import "HQAddModuleViewController.h"
#import "HQAddStyleViewController.h"
#import "HQEditSearchViewController.h"

@interface HQStyleSearchViewController ()<HQAddStyleModulViewDelegate>

@end

@implementation HQStyleSearchViewController

/**
 搜索框触发的方法
 
 @param searchBar 搜索框
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    if(searchBar.text.length == 0){
        [self searchStyle:@""];
    }else{
        [self searchStyle:searchBar.text];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSearchBar:@"搜索产品编号或风格"];
    
    // 下拉刷新
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self searchStyle:@""];
        [_collectionView.mj_header endRefreshing];
    }];
    [_collectionView.mj_header beginRefreshing];
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    _layout.itemSize = CGSizeMake(((SCREEN_WIDTH-25)/2) ,((SCREEN_WIDTH)/2)*1.4);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HQStyleSearchCell *cell = [HQStyleSearchCell cellWithCollectionView:collectionView withIndexPath:indexPath];
    cell.dataInfo = _dataSource[indexPath.row];
    cell.itemName.text = [NSString stringWithFormat:@"  %@  ",cell.dataInfo[@"s_name"]];
    cell.editItem.dataInfo = cell.dataInfo;
    cell.deleteItem.dataInfo = cell.dataInfo;
    [cell.editItem addTarget:self action:@selector(editItem:) forControlEvents:UIControlEventTouchUpInside];
    [cell.deleteItem addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
    [cell.itemIcon setImageWithURL:[NSURL URLWithString:cell.dataInfo[@"s_img"]]];
    return cell;
}

/**
 编辑样式搜索数据
 
 @param button 按钮
 */
-(void)editItem:(HQButton*)button{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定修改此条样式数据吗？ "  preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        HQAddStyleViewController *addStyleVc = [[HQAddStyleViewController alloc]init];
        addStyleVc.dataInfo = button.dataInfo;
        addStyleVc.pushType = @"EDIT";
        [self.navigationController pushViewController:addStyleVc animated:YES];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {}]];
    [self presentViewController:alertController animated:YES completion:nil];
}

/**
 删除样式数据

 @param button 按钮
 */
-(void)deleteItem:(HQButton *)button{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除此条样式数据吗？ "  preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam deleteStyleCmone:button.dataInfo[@"s_id"]] withMethod:[HQMethod stylecomDelCom] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1 || [sucData[kStatus]integerValue] == 0){
                [self searchStyle:@""];
                s.text = @"";
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {}]];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HQEditSearchViewController *editSearchVc = [[HQEditSearchViewController alloc]init];
    editSearchVc.dataInfo = _dataSource[indexPath.row];
    [self.navigationController pushViewController:editSearchVc animated:YES];
}

/**
 搜索样式数据
 */
-(void)searchStyle:(NSString *)searchText{
    _collectionView.backgroundView = nil;
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam StylecomIndexID:[UserModelManager userID] withSname:searchText] withMethod:[HQMethod StylecomIndex] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            if(_dataSource.count ==0){
                [self addNoneDataCollView:_collectionView withText:@"暂无样式数据，请新建样式"];
            }
            [_collectionView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 添加组件
 
 @param sender 添加组件
 */
- (IBAction)addModulItem:(id)sender {
    NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"HQAddStyleModulView" owner:nil options:nil];
    HQAddStyleModulView *photoView = objs[0];
    photoView.delegate = self;
    photoView.frame = [UIScreen mainScreen].bounds;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:photoView];
}

/**
 新建样式
 */
-(void)newStyle{
    HQAddStyleViewController *addStyleVc = [[HQAddStyleViewController alloc]init];
    [self.navigationController pushViewController:addStyleVc animated:YES];
}

/**
 新建组件
 */
-(void)newZuJian{
    HQAddModuleViewController *addModuleVc = [[HQAddModuleViewController alloc]init];
    [self.navigationController pushViewController:addModuleVc animated:YES];
}

@end
